#include "../header/geometry.h"

Vector Intersection(const Vector& a, const Vector& b, const Vector& c, const Vector& d) 
{
	double e = (a[0] - b[0]) * (c[1] - d[1]) - (a[1] - b[1]) * (c[0] - d[0]); 

	double u = (a[0] * b[1] - a[1] * b[0]);
	double v = (c[0] * d[1] - c[1] * d[0]); 

	double x = ( u * (c[0] - d[0]) - (a[0] - b[0]) * v ) / e; 
	double y = ( u * (c[1] - d[1]) - (a[1] - b[1]) * v ) / e; 

	return Vector(x,y,0.0);
}

// ============================================== //
// ================== TRIANGLE ================== //
// ============================================== //

Triangle::Triangle(const Vector& a, const Vector& b, const Vector& c)
{
	p[0] = a;
	p[1] = b;
	p[2] = c;
}

Triangle Triangle::Shrink(const double& ab,const double& bc, const double& ca)
{
	Vector sa = p[0] - Normalized((p[1] - p[0]) / Vector(0,0,1)) * ab;
	Vector sb = p[1] - Normalized((p[2] - p[1]) / Vector(0,0,1)) * bc;
	Vector sc = p[2] - Normalized((p[0] - p[2]) / Vector(0,0,1)) * ca;

	return Triangle(Intersection(sa, sa + (p[1] - p[0]), sb, sb + (p[2] - p[1])),
					Intersection(sb, sb + (p[2] - p[1]), sc, sc + (p[0] - p[2])),
					Intersection(sc, sc + (p[0] - p[2]), sa, sa + (p[1] - p[0])));
}

// ============================================== //
// ================= QUADRANGLE ================= //
// ============================================== //

Quadrangle::Quadrangle(const Vector& a,const Vector& b,const Vector& c,const Vector& d)
{
	p[0] = a;
	p[1] = b;
	p[2] = c;
	p[3] = d;
}

Quadrangle Quadrangle::Shrink(const double& ab,const double& bc, const double& cd, const double& da)
{
	Vector sa = p[0] - Normalized((p[1] - p[0]) / Vector(0,0,1)) * ab;
	Vector sb = p[1] - Normalized((p[2] - p[1]) / Vector(0,0,1)) * bc;
	Vector sc = p[2] - Normalized((p[3] - p[2]) / Vector(0,0,1)) * cd;
	Vector sd = p[3] - Normalized((p[0] - p[3]) / Vector(0,0,1)) * da;

	// On ajoute les hauteurs de bases
	sa[2] = p[0][2];
	sb[2] = p[1][2];
	sc[2] = p[2][2];
	sd[2] = p[3][2];

	return Quadrangle(	Intersection(sa, sa + (p[1] - p[0]), sd, sd + (p[0] - p[3])),
						Intersection(sb, sb + (p[2] - p[1]), sa, sa + (p[1] - p[0])),
						Intersection(sc, sc + (p[3] - p[2]), sb, sb + (p[2] - p[1])),
						Intersection(sd, sd + (p[0] - p[3]), sc, sc + (p[3] - p[2])));
}
